#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:HeWenjun
# datetime:2024/1/31 17:24
# software: PyCharm
from openpyxl import load_workbook


def replace_keyword(keyword, file_path, replacement):
    # 打开Excel文档
    wb = load_workbook(file_path)
    # 获取活动工作表
    ws = wb.active
    # 遍历工作表中的每一行
    for row in ws.iter_rows():
        # 遍历行中的每一个单元格
        for cell in row:
            # 如果单元格中有关键字
            if cell.value and keyword in str(cell.value):
                # 替换关键字
                cell.value = str(cell.value).replace(keyword, replacement)
                print(keyword + ' 已修改 ' + replacement)
    # 保存修改后的文档
    wb.save(file_path)
